using System;
using System.Collections;
using System.Collections.Generic;
using QFramework;
using Spine;
using Spine.Unity;
using UnityEngine;
using UnityEngine.UI;

//玩法开始前，等待学生进程的管理器
public class Wait : MonoBehaviour
{
    // Start is called before the first frame update
    // public Text Daojishi;
    public SkeletonGraphic skeletonGraphic;

    //去掉了默认计时
    //public int count = 300; //倒计时默认300秒

    private Boolean isStart = false;
    void Start()
    {
        //启动倒计时
        // StartCoroutine(DownTime(count));
    }

    private IEnumerator DownTime(int countDownTime)
    {
        while (countDownTime > 0)
        {
            // Daojishi.text = countDownTime.ToString() + "s";
            yield return new WaitForSeconds(1);
            countDownTime--;
        }
        //当倒计时结束时，执行相应操作
        Debug.Log("倒计时结束！");
        // Daojishi.text = "0s";
        //发出一个事件，通知其他脚本倒计时结束
        if (!isStart)
        {
            StartNow();
        }
    }

    // Update is called once per frame
    public void StartNow()
    {
       
        //播放close动画
        skeletonGraphic.AnimationState.SetAnimation(0, "open", false);
        //动画播放完成的回调
        skeletonGraphic.AnimationState.Complete += OnAnimationComplete;
        //隐藏描述和按钮
        transform.Find("desc").gameObject.SetActive(false);
        transform.Find("btn").gameObject.SetActive(false);
       
    }
    // 回调方法
    private void OnAnimationComplete(TrackEntry trackEntry)
    {
        Debug.Log("动画播放完成");
        // 在这里添加你的逻辑
         if  (isStart) return;
        isStart = true;
        SendGameStartEvent();
    }

    private void SendGameStartEvent()
    {
        string TiType = PlayerPrefs.GetString("TiKuGroup");
        string AnsTime = PlayerPrefs.GetString("AnsTimeGroup");
        string TiNum = PlayerPrefs.GetString("TiNumGroup");
        string _GameMode = PlayerPrefs.GetString(GameMode.Mode);
        GameType gameType = new()
        { 
            TiType = TiType,
            AnsTime = AnsTime,
            TiNum = TiNum,
            GameMode = _GameMode
        };
        //发出玩法开始的全局事件
        StringEventSystem.Global.Send(EventID.GameStarted,gameType);
        Debug.Log("发射玩法开始监听事件");
    }
}
